package cn.com.gridinfo.sooc.core.my.note.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.my.note.bean.MyNote;

/**
 * 我的笔记列表 适配器
 * Created by 刘晓晨 on 2015/12/22.
 */
public class MyNoteListAdapter extends UltimateViewAdapter<MyNoteListAdapter.ViewHolder> {

    public List<MyNote> myNoteList;
    Context context;

    public MyNoteListAdapter(List<MyNote> list, Context context) {
        this.myNoteList = list;
        this.context = context;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_note_list_item, parent, false);
        ViewHolder h = new ViewHolder(view, true);
        view.setOnClickListener(h);
        return h;
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(MyNote myNote, int position);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private MyNote myNote;

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= myNoteList.size() : position < myNoteList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            myNote = myNoteList.get(position);
            Picasso.with(context).load(myNote.getKcfm()).resize(context.getResources().getDimensionPixelSize(R.dimen.fengmian_width_in_mine),
                    context.getResources().getDimensionPixelSize(R.dimen.fengmian_height_in_mine)).placeholder(R.mipmap.course_placeholder)
                    .error(R.mipmap.course_placeholder).into(holder.kcfm);//加载头像
            holder.kcmc.setText(myNote.getKcmc());//加载课程名称
            holder.bjsl.setText("笔记: " + myNote.getBjsl() + "条");//加载笔记数量
        }
    }

    //加载更多
    public void loadmore(List<MyNote> list) {
        myNoteList.addAll(list);
        notifyDataSetChanged();
    }

    //刷新
    public void refresh(List<MyNote> list) {
        clear(myNoteList);
        myNoteList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 根据position删除数据
     *
     * @param position 位置
     */
    public void deleteByPosition(int position) {
        myNoteList.remove(position);
        notifyDataSetChanged();
    }

    /**
     * 修改数据
     *
     * @param position 位置
     * @param value    修改为value
     */
    public void updateByPosition(int position, int value) {
        myNoteList.get(position).setBjsl(value + "");
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getAdapterItemCount() {
        return myNoteList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder implements View.OnClickListener {
        ImageView kcfm;
        TextView kcmc, bjsl;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
//                //控件布局
                kcfm = (ImageView) itemView.findViewById(R.id.kcfm);//课程封面
                kcmc = (TextView) itemView.findViewById(R.id.kcmc);//课程名称
                bjsl = (TextView) itemView.findViewById(R.id.bjsl);//笔记数量
            }
        }

        @Override
        public void onClick(View v) {

            mOnItemClickLitener.OnItemClick(myNoteList.get(getAdapterPosition()), getAdapterPosition());
        }
    }
}
